package com.mxl.mvc.channel;

import java.io.PrintWriter;
import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.mxl.common.MD5Util;
import com.mxl.service.channel.CommCallbackProcessService;
import com.mxl.service.channel.CommCallbackRequest;

/**
 * 
 * @author mxl 卡渠道回调接口
 */
public class Pay19CardCallBackController extends AbstractController {
	private static final Logger logger = LoggerFactory
			.getLogger(Pay19CardCallBackController.class);

	private CommCallbackProcessService commCallbackProcessService;

	private String key;

	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.info("Pay19CardCallBackController callback! {}",
				request.getParameterMap());
		String output = "N";
		CommCallbackRequest request2 = new CommCallbackRequest();
		try {
			// 判断签名
			if (signCheck(request, request2)) {
				output = commCallbackProcessService.process(request2);
			}
		} catch (Exception ex) {
			logger.error("Pay19Card process card callback error!", ex);
		}
		if (output != null) {
			logger.info("Pay19Card response for card callback:{}", output);
			PrintWriter out = response.getWriter();
			out.print(output);
			out.flush();
			out.close();
		}
		return null;
	}

	// 判断签名是否正确
	private boolean signCheck(HttpServletRequest request,
			CommCallbackRequest request2) {

		request2.setOrderID(request.getParameter("order_id")); // yyyyMMdd
		String result = request.getParameter("result"); // 支付结果
		// request2.setChargeMoney(request.getParameter("r1"))// 结算金额
		request2.setPayMoney(request.getParameter("amount")); // 卡金额
		request2.setSeqId(request.getParameter("pay_sq")); // 渠道订单号
		if ("0".endsWith(request.getParameter("card_status1"))) {
			result = "Y";
		}
		request2.setErrcode(result + request.getParameter("card_code1"));
		request2.setState(Integer.valueOf(request.getParameter("card_status1")));
		request2.setFinishTime(request.getParameter("card_date1"));
		String verifystring = request.getParameter("verifystring"); // 验证摘要串
		String callback_sign_template = "version_id={0}&merchant_id={1}&order_id={2}&result={3}&order_date={4}&amount={5}&currency={6}&pay_sq={7}&pay_date={8}&count={9}&card_num1={10}&card_pwd1={11}&pc_id1={12}&card_status1={13}&card_code1={14}&card_date1={15}&r1={16}&merchant_key={17}";
		String signSource = MessageFormat.format(
				callback_sign_template,
				new Object[] { request.getParameter("version_id"),
						request.getParameter("merchant_id"),
						request2.getOrderID(), result,
						request.getParameter("order_date"),
						request2.getPayMoney(),
						request.getParameter("currency"), request2.getSeqId(),
						request.getParameter("pay_date"),
						request.getParameter("count"),
						request.getParameter("card_num1"),
						request.getParameter("card_pwd1"),
						request.getParameter("pc_id1"),
						request.getParameter("card_status1"),
						request.getParameter("card_code1"),
						request2.getFinishTime(), request.getParameter("r1"),
						key });
		request2.setCallBackContent(signSource + ",&verifystring="
				+ verifystring);
		logger.info("Pay19CardCallBackController callback signSource! {}",
				signSource);
		logger.info("Pay19CardCallBackController callback verifystring! {}",
				verifystring);
		return true;
		// String signInfo = MD5Util.md5Hex(signSource);
		// return signInfo.equals(verifystring);
	}

	public void setCommCallbackProcessService(
			CommCallbackProcessService commCallbackProcessService) {
		this.commCallbackProcessService = commCallbackProcessService;
	}

	public void setKey(String key) {
		this.key = key;
	}
}
